package com.ruoyi.mq.config;

import com.ruoyi.mq.bean.SysConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicQueueConfig {

    @Value("${topic.destination}")
    private String destination;


    @Value("${fanout.deployQueue}")
    private String deployQueue;
    @Value("${topic.routingKey}")
    private String routingKey;
    /**
     * 消息持久化
     */
    @Bean(name = "topic_queue")
    public Queue topic_queue() {
        return new Queue(destination,true);
    }
    //交换机持久化
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(SysConstant.NIOT_TOPIC_EXCHANGE,true,false);
    }

    @Bean
    Binding bindingExchangeTopicQueue1(@Qualifier("topic_queue") Queue topic_queue, TopicExchange exchange) {
        return BindingBuilder.bind(topic_queue).to(exchange).with(routingKey);
    }




    // 定义 fanout 交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(SysConstant.FANOUT_EXCHANGE, true, false);
    }

    // 队列定义
    @Bean
    public Queue queue() {
        return new Queue(deployQueue, true);
    }

    // 绑定队列到交换机
    @Bean
    public Binding bindingQueue2() {
        return BindingBuilder.bind(queue()).to(fanoutExchange());
    }

}
